/**
 * @作者:黎谢之
 * @创建时间：2021/7/15
 * @修改时间：2021/7/19
 */
package com.example.senior_bug.Service;

import com.example.senior_bug.EntityClass.Address;
import com.example.senior_bug.EntityClass.Business;
import com.example.senior_bug.EntityClass.Customer;
import com.example.senior_bug.ToolClass.Result;
import com.example.senior_bug.Repository.AddressRepository;
import com.example.senior_bug.Repository.BusinessRepository;
import com.example.senior_bug.Repository.CustomerRepository;
import com.example.senior_bug.enm.MsgId;
import com.example.senior_bug.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService {
    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private BusinessRepository businessRepository;

    /*用户删除地址
     * 7.19创建
     * 7.23修改
     * */
    public Result deleteAddress(int id) {
        //先判断是否存在该地址，防止删除null
        if (addressRepository.existsById(id)) {

            Address address = addressRepository.findById(id);//找到对应地址对象

            //判断是删除用户的还是商家的地址
            if (address.getCustomerId() != 0) {

                //首先在对应用户下地址列表中删除
                Customer customer = customerRepository.findById(address.getCustomerId());
                int index = customer.getAddresses().indexOf(address);
                customer.getAddresses().remove(index);
                customerRepository.saveAndFlush(customer);

                //在数据库中移除该地址对象
                addressRepository.delete(address);
                return ResultUtils.success(true);

            } else if (address.getBusinessId() != 0) {

                //首先在对应商家下地址列表中删除
                Business business = businessRepository.findById(address.getBusinessId());
                int index = business.getAddresses().indexOf(address);
                business.getAddresses().remove(index);
                businessRepository.saveAndFlush(business);

                //在数据库中移除该地址对象
                addressRepository.delete(address);
                return ResultUtils.success(true);

            } else {
                return ResultUtils.error(MsgId.ADDRESS_ERROR);
            }

        } else {
            return ResultUtils.error(MsgId.ADDRESS_NOT_FOUND);
        }
    }

    /*支付界面或用户地址页面获取地址列表
     * 7.15创建
     * 7.19修改
     * 7.23修改
     * */
    public Result getAddressByCustomerName(String CustomerName) {
        //先判断是否存在该用户，防止对null操作
        if (customerRepository.existsByName(CustomerName)) {
            //获取用户对象
            Customer customer = customerRepository.findByName(CustomerName);

            //获取该用户的地址列表，并根据数量返回
            List<Address> addresses = addressRepository.findAllByCustomerId(customer.getId());
            if (addresses.size() != 0) {
                return ResultUtils.success(addresses);
            } else {
                return ResultUtils.error(MsgId.ADDRESS_NOT_SET);
            }

        } else {
            return ResultUtils.error(MsgId.CUSTOMER_NOT_FOUND);
        }
    }

    /*商家地址页面获取地址列表
     * 7.15创建
     * 7.19修改
     * 7.23修改
     * */
    public Result getAddressByStoreName(String StoreName) {
        //先判断是否存在该商家，防止对null操作
        if (businessRepository.existsByStoreName(StoreName)) {
            //找到该商家对象
            Business business = businessRepository.findByStoreName(StoreName);
            //获取地址列表并根据数量返回
            List<Address> addresses = addressRepository.findAllByBusinessId(business.getId());
            if (addresses.size() != 0) {
                return ResultUtils.success(addresses);
            } else {
                return ResultUtils.error(MsgId.ADDRESS_NOT_SET);
            }

        } else {
            return ResultUtils.error(MsgId.BUSINESS_NOT_FOUND);
        }
    }

    /*用户添加地址
     * 7.15创建
     * 7.23修改
     * */
    public Result addNewAddress(Address address) {
        //先判断是用户添加地址还是商家添加地址
        if (address.getCustomerId() != 0) {
            //存储用户ID并存地址到数据库
            int id = address.getCustomerId();
            Address address1 = addressRepository.saveAndFlush(address);
            //将地址加入用户的地址列表完善关系表
            Customer customer = customerRepository.findById(id);
            customer.getAddresses().add(address1);
            customerRepository.saveAndFlush(customer);

            //判断关系是否成功建立
            if (customerRepository.findById(id).getAddresses().contains(address1)) {
                return ResultUtils.success();
            } else {
                return ResultUtils.error(MsgId.ADD_ADDRESS_FAILED);
            }

        } else if (address.getBusinessId() != 0) {
            //存储商家ID并存地址到数据库
            int id = address.getBusinessId();
            Address address1 = addressRepository.saveAndFlush(address);
            //将地址加入商家的地址列表完善关系表
            Business business = businessRepository.findById(id);
            business.getAddresses().add(address1);
            businessRepository.saveAndFlush(business);

            //判断关系是否成功建立
            if (businessRepository.findById(id).getAddresses().contains(address1)) {
                return ResultUtils.success();
            } else {
                return ResultUtils.error(MsgId.ADD_ADDRESS_FAILED);
            }
        } else {
            return ResultUtils.error(MsgId.ADDRESS_ERROR);
        }
    }

    /*修改收货地址函数
     * 7.16创建
     * 7.23修改
     * */
    public Result modifyAddress(Address address) {
        //先查找是否已经存在该地址
        if (addressRepository.existsById(address.getId())) {
            //获取地址对象
            Address address1 = addressRepository.findById(address.getId());
            int index;
            boolean flag;

            //判断是用户地址还是商家地址&&存储地址的INDEX
            if (address.getCustomerId() != 0) {
                Customer customer = customerRepository.findById(address1.getCustomerId());
                index = customer.getAddresses().indexOf(address1);
                flag = true;
            } else if (address.getBusinessId() != 0) {
                Business business = businessRepository.findById(address1.getBusinessId());
                index = business.getAddresses().indexOf(address1);
                flag = false;
            } else {
                return ResultUtils.error(MsgId.ADDRESS_ERROR);
            }


            //修改地址对象
            address1.setArea(address.getArea());
            address1.setAreaNum(address.getAreaNum());
            address1.setProvince(address.getProvince());
            address1.setProvinceNum(address.getProvinceNum());
            address1.setTown(address.getTown());
            address1.setTownNum(address.getTownNum());
            address1.setCity(address.getCity());
            address1.setCityNum(address.getCityNum());
            address1.setDetailInfo(address.getDetailInfo());
            address1.setPhoneNum(address.getPhoneNum());
            address1.setReceiverName(address.getReceiverName());

            //根据判断条件去修改对应角色的地址
            if (flag) {
                Customer customer = customerRepository.findById(address1.getCustomerId());
                customer.getAddresses().remove(index);
                customer.getAddresses().add(address1);
                customerRepository.saveAndFlush(customer);

            } else {
                Business business = businessRepository.findById(address1.getBusinessId());
                business.getAddresses().remove(index);
                business.getAddresses().add(address1);
                businessRepository.saveAndFlush(business);

            }
            //存储修改地址
            addressRepository.saveAndFlush(address1);
            return ResultUtils.success();
        } else {
            return ResultUtils.error(MsgId.ADDRESS_NOT_FOUND);
        }
    }
}
